<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="utf-8">
    <meta name="viewport"
          content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
    <title>Beehive</title>
    <link rel="stylesheet" th:href="@{/layui/css/layui.css}" media="all">
    <script th:src="@{/js/jquery-3.4.0.js}"></script>
    <style>
        /* 设置输入框的宽度 */
        .layui-input {
            width: 260px;
        }
    </style>

</head>
<body>

    <form class="layui-form" lay-filter="user" style="margin-top: 24px;">
        <div class="layui-form-item">
            <label class="layui-form-label">用户名</label>
            <div class="layui-input-block">
                <input type="text" name="username" autocomplete="off" class="layui-input" disabled>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">姓名</label>
            <div class="layui-input-block">
                <input type="text" name="realName" autocomplete="off" class="layui-input" disabled>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">性别</label>
            <div class="layui-input-block">
                <input type="radio" name="gender" value="1" title="男" disabled>
                <input type="radio" name="gender" value="0" title="女" disabled>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">手机号码</label>
            <div class="layui-input-block">
                <input type="text" name="phoneNum" autocomplete="off" class="layui-input" disabled>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">电子邮箱</label>
            <div class="layui-input-block">
                <input type="text" name="email" autocomplete="off" class="layui-input" disabled>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">新密码</label>
            <div class="layui-input-block">
                <input type="text" id="newPass" name="newPass" autocomplete="off" class="layui-input" required>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">确认新密码</label>
            <div class="layui-input-block">
                <input type="text" id="reNewPass" name="reNewPass" autocomplete="off" class="layui-input" required>
            </div>
        </div>
        <div class="layui-form-item">
            <div class="layui-input-block">
                <div id="submitBtn" class="layui-btn">修改</div>
                <div id="resetBtn" class="layui-btn layui-btn-primary">重置</div>
            </div>
        </div>
    </form>

<script th:src="@{/layui/layui.js}"></script>
<script>

    // 保存url传参
    let user = {};

    layui.use('form', function () {
        var form = layui.form;

        //各种基于事件的操作，下面会有进一步介绍
        form.val("user", {
            "username": user.username,
            "realName": user.realName,
            "gender": user.gender,
            "phoneNum": user.phoneNum,
            "email": user.email,
        });
    });

    // JS函数(DOM加载完成时执行)获取参数，获取url参数赋值给user对象
    $(function () {
        // 获取到的是JSON对象的格式字符串[{}]
        let str = unescape(window.location.search.substring(1).split("=")[1]);
        // 如果有是数组形式JSON，则去掉中括号，因只有一个对象
        if (str.indexOf("[") != -1) {
            str = str.substring(1, str.length - 1);
        }
        user = JSON.parse(str);
        console.log(JSON.stringify(user));
    });

    // 单击重置按钮，清空修改密码框
    $("#resetBtn").on("click", function () {
        $("#newPass").val("");
        $("#reNewPass").val("");
    });

    // 单击修改按钮，验证新密码
    $("#submitBtn").on("click", function () {
        let pass = checkPass();
        if (pass === "" || pass == null) {
            return;
        }
        console.log("into checkPass2()")
        let pass2 = checkPass2();
        console.log(pass2);
        if (pass2 === "" || pass2 === null || pass2 === undefined) {
            console.log("pass2 is null")
            return;
        }
        // 封装请求数据
        let map = {};
        map.id = user.id;
        map.password = pass;
        // Ajax请求
        $.ajax({
            url: "/admin/updateUserPass",
            type: "post",
            data: JSON.stringify(map),
            contentType: "application/json;charset=UTF-8",
            dataType: "json",
            success: function (data) {
                // res表示受影响的行数
                if (data.res === 1) {
                    layer.msg("成功修改了用户ID：" + user.id + " 的密码！", {icon: 6, time: 1000});
                    setTimeout(jump, 3000);
                } else {
                    layer.msg("修改密码失败/(ㄒoㄒ)/~~", {icon: 5, time: 1000});
                }
            }

        });
    });

    // 检查密码
    function checkPass() {
        let pass = $("#newPass");
        let value = pass.val();
        let len = value.length;
        let regex = /(.*[a-zA-z]*.*)+(.*[0-9a-zA-z]*.*)+/;
        if (len === 0) {
            layer.msg("请输入新密码！", {icon: 5, time: 900});
            return;
        }
        // 限制长度
        if (len < 8 || len > 20) {
            layer.msg("密码长度必须在8-20位之间！", {icon: 0, time: 900});
            return;
        }
        // test() 返回 Boolean，查找对应的字符串中是否存在模式
        if (!regex.test(value)) {
            layer.msg("密码至少8位，包含字母、数字！", {icon: 0, time: 900});
            return;
        }
        return value;
    }

    // 确认密码
    function checkPass2() {
        let pass = $("#newPass");
        let value = pass.val();
        let pass2 = $("#reNewPass");
        let value2 = pass2.val();
        if (value2.length === 0) {
            layer.msg("请输入确认密码！", {icon: 0, time: 900});
            return;
        }
        if (!(value === value2)) {
            layer.msg("两次密码输入不一致！", {icon: 0, time: 900});
            return;
        }
        return value2;
    }

    // 回到用户管理主页面
    function jump() {
        window.open("/admin/userList", "main");
    }
</script>
</body>
</html>